package com.tubeonfire.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser(); // or req.getUserPrincipal()		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		if (user != null) {
			out.println("Hello <i>" + user.getNickname() + "</i>!");
			out.println("[<a href=\""
					+ userService.createLogoutURL(request.getRequestURI())
					+ "\">sign out</a>]");
		} else {
			request.getRequestDispatcher("/user_login.jsp").forward(request,
					response);
		}
	}
}
